{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "ename": "ModuleNotFoundError",
     "evalue": "No module named 'tensorflow.examples.tutorials'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mModuleNotFoundError\u001b[0m                       Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-1-ee5c50fc3d95>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mtensorflow\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0mtf\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[1;32mfrom\u001b[0m \u001b[0mtensorflow\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mexamples\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtutorials\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmnist\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0minput_data\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      3\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mnumpy\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mmatplotlib\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpyplot\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0mplt\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[0mget_ipython\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrun_line_magic\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'matplotlib'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'qt5'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'tensorflow.examples.tutorials'"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "from tensorflow.examples.tutorials.mnist import input_data\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib qt5\n",
    "\n",
    "mnist = input_data.read_data_sets(r\"C:\\Users\\zdwxx\\Downloads\\Compressed\\MNIST_data\", one_hot=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "batch_size = 256\n",
    "n_input = 784"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第 0001 个循环，loss是: 0.073095858\n",
      "第 0006 个循环，loss是: 0.048142403\n",
      "第 0011 个循环，loss是: 0.042466227\n",
      "第 0016 个循环，loss是: 0.040883873\n",
      "第 0021 个循环，loss是: 0.040777501\n",
      "第 0026 个循环，loss是: 0.039862007\n",
      "第 0031 个循环，loss是: 0.037560124\n",
      "第 0036 个循环，loss是: 0.036317248\n",
      "第 0041 个循环，loss是: 0.037167419\n",
      "第 0046 个循环，loss是: 0.036537968\n"
     ]
    }
   ],
   "source": [
    "def encoder(x):\n",
    "    # 首先压缩成256，然后压缩成128\n",
    "    weights1 = tf.Variable(tf.random_normal([784, 256]))\n",
    "    biases1 = tf.Variable(tf.random_normal([256]))\n",
    "    layer1 = tf.nn.sigmoid(tf.matmul(x, weights1) + biases1)\n",
    "    # 第二层，压缩成128\n",
    "    weights2 = tf.Variable(tf.random_normal([256, 128]))\n",
    "    biases2 = tf.Variable(tf.random_normal([128]))\n",
    "    layer2 = tf.nn.sigmoid(tf.matmul(layer1, weights2) + biases2)\n",
    "    \n",
    "    return layer2\n",
    "\n",
    "def decoder(x):\n",
    "    # 首先先从128还原成256，然后再从256还原会784\n",
    "    weights1 = tf.Variable(tf.random_normal([128, 256]))\n",
    "    biases1 = tf.Variable(tf.random_normal([256]))\n",
    "    layer1 = tf.nn.sigmoid(tf.matmul(x, weights1) + biases1)\n",
    "    # 从256还原成784\n",
    "    weights2 = tf.Variable(tf.random_normal([256, 784]))\n",
    "    biases2 = tf.Variable(tf.random_normal([784]))\n",
    "    layer2 = tf.nn.sigmoid(tf.matmul(layer1, weights2) + biases2)\n",
    "    \n",
    "    return layer2\n",
    "\n",
    "# 定义一个placeholder\n",
    "X = tf.placeholder(tf.float32, [None, 784])\n",
    "\n",
    "encoder_op = encoder(X)\n",
    "decoder_op = decoder(encoder_op)\n",
    "\n",
    "# Prediction\n",
    "prediction = decoder_op\n",
    "\n",
    "# 定义Optimizer\n",
    "loss = tf.reduce_mean(tf.square(prediction - X))\n",
    "train_step = tf.train.AdamOptimizer(0.01).minimize(loss)\n",
    "\n",
    "# 初始化变量initial\n",
    "init = tf.global_variables_initializer()\n",
    "\n",
    "with tf.Session() as sess:\n",
    "    \n",
    "    sess.run(init)\n",
    "    total_batch = mnist.train.num_examples // batch_size\n",
    "    \n",
    "    # 开始训练\n",
    "    for epoch in range(50):\n",
    "        \n",
    "        for i in range(total_batch):\n",
    "            \n",
    "            batch_xs, batch_ys = mnist.train.next_batch(batch_size)\n",
    "            # 训练并获得loss值\n",
    "            _, c = sess.run([train_step, loss], feed_dict={X : batch_xs})\n",
    "        \n",
    "        if epoch % 5 == 0:\n",
    "            print(\"第 %04d 个循环，loss是: %.9f\" %(epoch + 1, c))\n",
    "    \n",
    "    encode_decode = sess.run(prediction, feed_dict={X : mnist.test.images[:10]})\n",
    "    \n",
    "    f, a = plt.subplots(2, 10, figsize=(10, 2))\n",
    "    for i in range(10):\n",
    "        a[0][i].imshow(np.reshape(mnist.test.images[i], (28, 28)))\n",
    "        a[1][i].imshow(np.reshape(encode_decode[i], (28, 28)))\n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
